const http = require('http');
const app = http.createServer();
const url = require('url');
const querystring = require('querystring');
const mongooose = require('mongoose');
mongooose.connect('mongodb://localhost/playground', {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log('数据库连接成功');
}).catch(err => {
    console.log(err, '数据库连接失败');
})
const myinfor = new mongooose.Schema({
    name: {
        required: true,
        type: String,
        minlength: [2, '最小值不能小于2'],
        maxlength: [10, '最大值不能大于10']
    },
    age: {
        type: Number,
        min: [5, '年龄最小不能低于5'],
        max: [100, '年龄最大不能大于100']
    },
    sex: String,
    email: String,
    hobbies: [String],
    collage: String,
    enterDate: {
        type: Date,
        default: Date.now
    }
})
const myInfor = mongooose.model('Myinfor', myinfor);

const template = require('art-template');
const path = require('path');
template.defaults.root = path.join(__dirname, 'views');
const dateFormat = require('dateformat');
template.defaults.imports.myformat = dateFormat;

const serveStatic = require('serve-static');
const serve = serveStatic(path.join(__dirname, 'public'));





const Router = require('router');
const router = Router();
router.get('/list', async (req, res) => {
    let allData = await myInfor.find({});
    let obj = {
        myallData: allData
    }
    let html = template('list.art', obj);
    res.end(html)
})
router.get('/add', (req, res) => {
    const html = template('index.art', {});

    res.end(html)
})
router.post('/add', (req, res) => {
    let formData = '';
    req.on('data', (params) => {
        formData += params
    });
    req.on('end', async () => {
        // console.log(formData);  
        let data = querystring.parse(formData);
        // console.log(data);
        let successData = await myInfor.create(data);
        // console.log(successData);
    })
    res.writeHead(301, {
        location: '/list'
    })
    res.end()
})
//删除功能
router.get('/remove', async (req, res) => {
    let myid = url.parse(req.url, true).query.id;
    let delData = await myInfor.findOneAndDelete({
        _id: myid
    })
    res.writeHead(301, {
        location: '/list'
    })
    res.end()
})
//修改功能
router.get('/modify', async(req, res) => {
    modfiyId= url.parse(req.url, true).query.id;
    let findData = await myInfor.findOne({
        _id: modfiyId
    })
    let html = template('index2.art', findData)
    res.end(html)
})
router.post('/modify', (req, res) => {
    let myid = modfiyId;
    let modifyData = '';
    req.on('data', (params) => {
        modifyData += params
    })
    req.on('end', async () => {
        let obj = querystring.parse(modifyData)
        // console.log(obj);
        let modfiyRes = await myInfor.updateOne({
            _id: myid
        }, obj)
        res.writeHead(301, {
            location: '/list'
        })
        res.end()
    })
})

app.on('request', (req, res) => {
    router(req, res, () => {
        console.log('路由触发了');
    })
    serve(req, res, () => {
        console.log('css静态托管');
    })

    // res.end('ok')
})
app.listen(8888, () => {
    console.log('http://localhost:8888');
})














